<template>
  <el-container class="el-home">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img class="pic" src="../assets/logo.webp" alt />
        <span @click="showBigScreen" style="cursor: pointer"
          >快递物流管理系统</span
        >
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          class="menus"
          background-color="#FFFFFF"
          active-text-color="#4987FF"
          text-color="#333333"
          unique-opened
          :collapse="isCollapse"
          router
          :collapse-transition="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <span v-if="!isCollapse"> &nbsp;&nbsp; </span>
              <i class="iconfont icon-users"></i>
              &nbsp;
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import axios from "axios";
import store from "./store.js";
export default {
  data() {
    return {
      // 菜单栏列表
      menulist: [],
      objList: {
        1: "iconfont icon-users",
        2: "iconfont icon-tijikongjian",
        3: "iconfont icon-danju",
        4: "iconfont icon-shangpin",
        5: "iconfont icon-baobiao",
      },

      //是否折叠，默认不折叠
      isCollapse: false,

      //被激活的链接地址
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = this.$route.path;
  },
  methods: {
    showBigScreen() {
      this.$router.push({ path: "/big" });
    },
    //  退出登录功能的实现
    logout() {
      //01-清空sessionStorage 02-跳转到login页面
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取侧边栏数据
    async getMenuList() {
      // const { data: res } = await this.$http.get("/menus");
      let { data: res } = await axios.get("http://localhost:8080/menus");
      if (res.code != 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
      console.log(this.menulist);
    },
    // 点击按钮，切换菜单的折叠与展开,通过取反来控制折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
      store.commit("setCollapse", this.isCollapse);
    },
  },
  watch: {
    "$route.path"(newvalue) {
      this.activePath = newvalue;
    },
  },
};
</script>
<style lang="less" scoped>
.el-home {
  height: 100%;
}
.el-header {
  background-color: #4381e6;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #ffffff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
  }
}
.pic {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}
.el-aside {
  background-color: #ffffff;
  /deep/.el-menu .el-menu-item:hover {
    outline: 0 !important;
    color: #fff;
    background: #f2f5ff !important;
  }
  /deep/.el-menu .el-menu-item.is-active {
    color: #fff;
    background: #f2f5ff !important;
  }
  /deep/ .el-submenu__title:hover {
    color: #fff !important;
    i {
      color: #fff !important;
    }
  }
  .el-submenu /deep/.el-submenu__title:hover {
    color: #fff;
    background: #96a1fe !important;
  }
}
.el-main {
  background-color: #f4f8ff;
}
.el-menu {
  border: none;
}
.toggle-button {
  background-color: #a0a7af;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>